<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use ParsedownExtra;

/**
 * App\Models\Topic
 *
 * @property int $id
 * @property string $title 帖子的标题
 * @property string $content 帖子的内容
 * @property int $user_id
 * @property int $cat_id
 * @property string $is_hot 是否是热门
 * @property int $sort 排序
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Database\Factories\TopicFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Topic newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic query()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereCatId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereIsHot($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereUserId($value)
 * @property-read \App\Models\Cat $cat
 * @property-read \App\Models\User $user
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $topicUsers
 * @property-read int|null $topic_users_count
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Comment> $comments
 * @property-read int|null $comments_count
 * @mixin \Eloquent
 */
class Topic extends Model
{
    use HasFactory;
    protected $fillable = ['title','content','sort','is_hot','cat_id'];
    public function getHtmlAttribute(){
        $Extra = new ParsedownExtra();
        return $Extra->text($this->content);
    }
    public function user(){
        return $this->belongsTo(User::class);
    }
    public function cat(){
        return $this->belongsTo(Cat::class);
    }
    public function topicUsers(){
        return $this->belongsToMany(User::class,'favorites')->withTimestamps();
    }

    public function comments(){
        return $this->hasMany(Comment::class);
    }

}
